package itheima.com.myreflect3;

import java.lang.reflect.Constructor;

public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException {
        //clazz.getConstructors() 返回所有     公共构造（public）方法对象的数组
        //clazz.getDeclaredConstructors() 返回所有     构造方法对象的数组
        //clazz.getConstructor()  返回    单个    公共构造方法对象
        //clazz.getDeclaredConstructor()  返回    单个构造方法
//        method1();
//        method2();
//        method3();
//        method4();
//        先用类获取对象名
//        再用对象名点方法获取各种无参与有参构造
    }

    private static void method4() throws ClassNotFoundException, NoSuchMethodException {
        Class clazz = Class.forName("itheima.com.myreflect3.Student");
        Constructor constructors = clazz.getDeclaredConstructor();
        System.out.println(constructors);

        Constructor constructor = clazz.getDeclaredConstructor(String.class);
        System.out.println(constructor);

        Constructor constructor0 = clazz.getDeclaredConstructor(String.class,int.class);
        System.out.println(constructor0);
    }

    private static void method3() throws ClassNotFoundException, NoSuchMethodException {
        //返回单个公共构造方法对象
        Class clazz = Class.forName("itheima.com.myreflect3.Student");
        //小括号中一定要跟构造方法的形参保持一致
        Constructor constructor = clazz.getConstructor();
        System.out.println(constructor);

        Constructor constructor1 = clazz.getConstructor(String.class, int.class);
        System.out.println(constructor1);

        //不存在会报错
        Constructor constructor2 = clazz.getConstructor(int.class);
        System.out.println(constructor2);
    }

    private static void method2() throws ClassNotFoundException {
        //clazz.getDeclaredConstructors() 返回所有     构造方法对象的数组
        Class clazz = Class.forName("itheima.com.myreflect3.Student");
        Constructor[] constructors = clazz.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
    }

    private static void method1() throws ClassNotFoundException {
        //clazz.getConstructors() 返回所有公共构造方法对象的数组
        Class clazz = Class.forName("itheima.com.myreflect3.Student");
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
    }
}
